package com.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.sfzy.entity.Answer;
import com.sfzy.service.AnswerService;
import com.sfzy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("answer")
public class AnswerController {
    @Autowired
    private AnswerService answerService;
    //增
    @PostMapping
    public R addAnswer(@RequestBody Answer answer) {
        int i = answerService.addAnswer(answer);
        if(i == 0){
            return R.fail("增加失败！");
        }
        return R.ok("增加成功！");
    }
    //删
    @DeleteMapping("{id}")
    public R deleteAnswer(@PathVariable("id") int id) {
        int i = answerService.deleteAnswer(id);
        if(i == 0){
            return R.fail("删除失败！");
        }
        return R.ok("删除成功！");
    }
    //改
    @PutMapping
    public R updateAnswer(@RequestBody Answer answer) {
        int i = answerService.updateAnswer(answer);
        if(i == 0){
            return R.fail("修改失败！");
        }
        return R.ok("修改成功！");
    }
    //根据id查
    @GetMapping("{id}")
    public R getAnswerById(@PathVariable("id") int id) {
        Answer answerById = answerService.getAnswerById(id);
        return R.ok(answerById);
    }
    //分页查
    @GetMapping("page")
    public R getAllAnswer(Answer answer,
                          @RequestParam(required = false,defaultValue = "1") int pageNum,
                          @RequestParam(required = false,defaultValue = "5") int pageSize){
        PageInfo<Answer> answers = answerService.getAnswers(answer, pageNum, pageSize);
        return R.ok(answers);
    }
}
